﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using KTVConsole2.Util;
using System.IO;
using KTVConsole2.Properties;

namespace KTVConsole2.UI
{
    /// <summary>
    /// Interaction logic for Config.xaml
    /// </summary>
    public partial class Config : Window
    {
        public Config()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            string a = Settings.Default.dbpath;
            if (string.IsNullOrEmpty(a.Trim()))
            {
                Settings.Default.dbpath = Environment.CurrentDirectory;
                Settings.Default.Save();
                txtDbPath.Text = Settings.Default.dbpath;
            }
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            switch(e.Key)
            {
                case Key.Escape:
                    this.Close();
                    break;
            }
        }

        private void btnRescan_Click(object sender, RoutedEventArgs e)
        {
            var a = txtKtvLibPath.Text;
            if (!Directory.Exists(a))
            {
                Util.Util.TellUser("目录不存在！");
                return;
            }
            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(a);
            var opt = cbIncludeSubDirs.IsChecked==true?SearchOption.AllDirectories:SearchOption.TopDirectoryOnly;
            ProgressBar1.Minimum = 0;
            ProgressBar1.Maximum = MainWindow.Engine.GetTotalSongsInDir(dir, opt);
            ProgressBar1.Value = 0;
            UpdateProgressBarDelegate updateProcessDelegate = new UpdateProgressBarDelegate(ProgressBar1.SetValue);
            MainWindow.Engine.ScanSongs(dir, opt, updateProcessDelegate);
        }

        //Create a Delegate that matches the Signature of the ProgressBar's SetValue method
        

        private void btnRenameFileByKsc_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnExplorer_Click(object sender, RoutedEventArgs e)
        {
            CommonDialogWrappers.BrowseForFolderDialog dlg = new CommonDialogWrappers.BrowseForFolderDialog();
            dlg.Title = "请选择卡拉OK文件所在目录";
            if (true == dlg.ShowDialog())
                txtKtvLibPath.Text = dlg.SelectedFolder;
        }

        private void ProgressBar1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            int value = (int)(ProgressBar1.Value / ProgressBar1.Maximum * 100);
            label1.Content = string.Format("扫描进度：{0}%", value);
            if (ProgressBar1.Value == ProgressBar1.Maximum)
                Util.Util.TellUser("扫描完成！");
        }

        private void BtnConfirmDb_Click(object sender, RoutedEventArgs e)
        {
            Settings.Default.dbpath = txtDbPath.Text;
            Settings.Default.Save();
            if (Directory.Exists(txtDbPath.Text))
            {
                KTVConsole2.Core.KtvEngine.SetXmlFilePath(txtDbPath.Text + @"/songsdb.xml");
                MainWindow.Engine = KTVConsole2.Core.KtvEngine.GetInstance();
                KTVConsole2.Core.SongPlayedManager.XmlFilePath = txtDbPath.Text + @"/songplayed.xml";
                MainWindow.Sm = KTVConsole2.Core.SongPlayedManager.GetInstance();
                this.Close();
            }
            else
                Util.Util.TellUser("目录不存在");
        }

        private void btnExplorerDb_Click(object sender, RoutedEventArgs e)
        {
            CommonDialogWrappers.BrowseForFolderDialog dlg = new CommonDialogWrappers.BrowseForFolderDialog();
            dlg.Title = "请选择数据库文件所在目录";
            if (true == dlg.ShowDialog())
                txtDbPath.Text = dlg.SelectedFolder;
        }
    }
}
